Skip to content

Conversation

oliveror
Copy link
Member

Overlaps: #123 (form limits) also changes RecipeCreatePage.tsx and its tests, in different parts. #122 (API limits) changes other functions in recipeRepository.ts and the test:api script. Whichever merges second needs an update from dev.

Changes

Concurrent edits

  • updateRecipe takes an optional expectedUpdatedAt. The editor sends the updatedAt it loaded.
  • If the recipe has changed since (another tab or device), the API refuses with code CONFLICT and overwrites nothing. The check runs under the row lock owned() already takes, and compares updatedAt in the format the API serves (milliseconds, UTC).
  • The form keeps the user's changes and explains how to keep them: save a draft, reload, then restore the draft (it is marked "Outdated draft").
  • Clients that don't send the version (e.g. GraphiQL) behave as before.

Unsaved changes

  • The form counts as unsaved when it differs both from how it was opened and from the draft this browser holds for it (saved or restored here).
  • Reloading, closing the tab or typing an address then gets the browser's own prompt (beforeunload).
  • Links and Back/Forward inside the app get a Leave without saving? dialog with Stay / Leave (useBlocker). This needs a data router, so main.tsx now uses createBrowserRouter with one catch-all route that renders the existing <Routes>. Nothing else about routing changes.
  • A successful save navigates away without asking.

Drafts and sign-out

  • Sign out first asks when it would delete saved drafts ("Your 2 saved drafts in this browser will be deleted") or close the editor with unsaved changes. If neither applies, it signs out straight away as before.
  • Save draft asks Replace saved draft? when a draft was offered but not restored or discarded, or when another tab saved one in the meantime.

Return after login

  • Opening /recipes/<id>/edit while logged out now returns to that editor after login. Only exact /recipes/<uuid>/edit paths are accepted; everything else still falls back to /.

Validation

  • npm run check passes (284 unit tests). New tests:
    • the update sends expectedUpdatedAt, and on CONFLICT the form keeps its contents and shows the message
    • Replace saved draft?: Keep it leaves the stored draft, Replace draft overwrites it
    • beforeunload is cancelled only while changes are unsaved (not after Save draft)
    • with a data router: a link away shows the dialog, Stay keeps the text, Leave navigates, and no dialog appears after publishing
    • sign-out lists drafts and unsaved changes, Stay signed in changes nothing, confirming deletes only this user's drafts, and a single draft is described in the singular
    • return paths: the edit route is allowed, and look-alike, traversal and protocol-relative paths are rejected
    • countUserDrafts()
  • test:api (PostgreSQL 16): tab A saves, tab B's save from the old version gets CONFLICT and the title stays as tab A saved it; saving with the current version, or without one, works.
  • The full Playwright suite (42 tests) passes locally, including the real-database scenarios and the Back/scroll-position test after the router change.

AI usage

Written with Claude Code (Anthropic) to address a teammate's issue. Please review the diff as you would any other contribution.

Closes #121

- updateRecipe takes an optional expectedUpdatedAt. The editor sends the
  version it loaded; if the recipe changed since (another tab or device)
  the API refuses with CONFLICT under the row lock, and the form keeps the
  user's changes with an explanation of how to keep them.
- Unsaved changes (form differs from how it opened and from the draft in
  this browser) trigger the browser's prompt on reload/close and, through
  a data router (createBrowserRouter) and useBlocker, a Leave/Stay dialog
  for in-app navigation. A successful save navigates away unasked.
- Sign-out asks first when it would delete saved drafts or close the
  editor with unsaved changes, and says which.
- Save draft asks before replacing a draft that was offered but not
  restored or discarded, or that another tab saved meanwhile.
- After logging in from /recipes/<id>/edit the user returns to that
  editor (strict UUID pattern, no other paths).

Closes #121
@oliveror oliveror requested a review from a team as a code owner November 17, 2026 15:46
The redirect test waited 1 s (the default) for the lazy-loaded Log in
page. As the file's first test it pays the cold import, which took 1.6 s
on the CI runner once the header imported the confirmation dialog.

Refs #121
Confirming sign-out clears the shared unsaved flag so the editor does not
ask again while it closes. If sign-out then failed, the flag stayed off
and the editor's leave and reload prompts were disabled for the rest of
the session. The previous value is now restored on failure.

Refs #121
@oliveror
Copy link
Member Author

Two follow-up commits after opening this PR:

  • 08147c6: RequireAuth.test.tsx now waits up to 5 s for the lazy-loaded Log in page in the redirect test. On CI it took 1.6 s, the file's first test pays the cold import, and the default wait is 1 s. The header now imports the confirmation dialog, which makes that first import a little heavier.
  • c116072: if sign-out failed after the user confirmed losing unsaved changes, the shared flag stayed off and the editor's leave and reload prompts stopped working. The previous value is now restored when sign-out fails. Test added next to the existing failed-logout test.

Copy link
Member

@olestan olestan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. Consider making these improvements:

  • Pending images (RecipeCreatePage.tsx:229–230): Selecting an image without changing other fields doesn't mark the form as unsaved. Reloading can discard pending image work without warning.
  • Draft data loss (RecipeCreatePage.tsx:229–230): If another tab replaces or deletes a saved draft, the editor still considers the form saved. Users can leave without a warning and lose their changes. The dirty state should account for external draft changes.

# Conflicts:
#	apps/web/src/features/recipes/RecipeCreatePage.test.tsx
#	apps/web/src/features/recipes/RecipeCreatePage.tsx
#	scripts/test-recipe-api.ts
…where

Choosing an image opens the crop dialog and uploads it later, and until
then nothing in the form differs, so reloading or leaving discarded the
pending image without a warning. A picked image that is not uploaded yet
now counts as an unsaved change.

The editor also remembered the draft it had saved or restored and
treated the form as kept for good. If another tab replaced or deleted
that draft, leaving still passed without a warning and the changes were
lost. It now reads the draft stored in this browser whenever it changes
(storage event, saving, discarding) and compares the form against that.
The test types and drags through the whole form and took about 4.95 s on the CI runner against vitest's 5 s default, timing out once. Give it 15 s.
@oliveror
Copy link
Member Author

Follow-up commits addressing review:

  • 761bc0c: A picked image that is not uploaded yet now counts as an unsaved change, so reloading or leaving warns instead of silently dropping it. The editor also no longer trusts a cached "saved" snapshot: it re-reads the draft stored in this browser on the storage event, after saving and after discarding. A draft replaced or deleted in another tab now makes the form unsaved again. Tests added for both: "counts a chosen image as unsaved before it is uploaded" and "warns when another tab replaces or deletes the saved draft".
  • d467a09: Gives the drag-reorder test a 15 s timeout. It took about 4.95 s on CI against vitest's 5 s default and timed out once. No behaviour change.

Also merged dev (#98, #123) to resolve the conflicts in scripts/test-recipe-api.ts, RecipeCreatePage.tsx and RecipeCreatePage.test.tsx, keeping the tests from both sides.

@oliveror oliveror merged commit 3586c34 into dev Nov 17, 2026
2 checks passed
@oliveror oliveror deleted the fix/recipe-edit-safety branch November 20, 2026 15:31
Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants